<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/common/taglibs.jsp" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html>
<html lang="en">
	<head>
		<%@ include file="/common/common-css.jsp" %>
		<%@ include file="/discovery/discovery-css.jsp" %>
		<%@ include file="/common/common-js.jsp" %>
		
		<style type="text/css">
			body {
				background: white;
			}
			.item_title {
				font-size: 20px;
			}
			.text_input {
				width: 300px;
			}
			#ios_lockkey_show {
				color: red;
				font-size: 15px;
			}
			#message_show {
				color: red;
				font-size: 15px;
			}
			#button_container {
				text-align: center;
			}
			#send_group_user_container {
				display: none;
			}
			#provinces {
				display: none;
			} 
			/*toast*/
			
			#toast_container {
				position: fixed;
				width: 100%;
				bottom: 70px;
				left: 0;
				text-align: center;
				display: none;
			}
			#toast_tips {
				padding: 5px 20px;
				border-radius: 20px;
				background: #000;
				color: #fff;
				opacity: 0.7;
				display: inline-block;
			}
		
			.load_bg{background:#000;opacity:0.1;left:0px;top:0px;position:fixed;height:100%;width:100%;overflow:hidden;z-index:10000;}
			.load_img{left:50%;top:180px;position:fixed;height:50px;overflow:hidden;z-index:20000;}
			
			div{
				padding: 4px;
			}
			select{
				height: 34px;
			}
			
		</style>
	</head>
	<body>
		<div  class="load_bg" style="display:none;" ></div>
		<img src="http://img.xmiles.cn/commentloading.gif" class="load_img" style="display:none"/>
		<div class="item_title">IOS通知栏内容：</div>
		<div id="ios_lockkey_show"></div>
		<br/>
		<div class="item_title">消息体：</div>
		<div id="message_show"></div>
		<br/>
		<div>
			<div class="item_title">推送产品：</div>
			<select id="message_send_product">
				<option value="8000">贷款之家</option>
				<option value="8001">急速借钱</option>
				<option value="8002">贷款之家iOS马甲</option>
				<option value="8003">手机现金侠</option>
				<option value="8004">借了么</option>
				<option value="8300">速来贷</option>
				<option value="8005">贷款之家新iOS</option>
				<option value="8007">菠萝贷8007</option>
				<option value="8015">小花贷款</option>
				<option value="8016">万能借钱</option>
				<option value="8017">西瓜借钱</option>
				<option value="8018">全能贷款</option>
				<option value="8301">芝麻借款</option>
				<option value="8400">菠萝小钱</option>
				<option value="8200">香蕉信用卡管家</option>
				<option value="8201">香蕉还卡提醒工具</option>
				<option value="8401">菠萝汇率</option>
				<option value="8402">菠萝贷款助手</option>
				<option value="8403">车贷助手</option>
				<option value="8404">收钱助手</option>
				<option value="8500">公积金</option>
				<option value="8700">银河黑卡</option>
				<option value="8900">西瓜借条</option>
				<option value="8404">收钱呗ios</option>
				<option value="8405">收钱呗android</option>
				<option value="8202">布拿拿管卡</option>
				<option value="8407">几多钱</option>
				<option value="8408">秒速D</option>
				<option value="8409">记一点</option>
				<option value="8410">纪念日</option>
				<option value="8411">喝水打卡</option>
				<option value="8412">菠萝贷_8412</option>
				<option value="8413">小时光</option>
				<option value="8414">记账宝</option>
				<option value="8415">菠萝贷8415</option>
				<option value="8416">幸运金</option>
				<option value="8417">帮你挑</option>
				<option value="8418">精明记</option>
				<option value="8203">布拿拿管卡_8203</option>
				<option value="8420">时时金</option>
				<option value="8419">心愿钱罐</option>
				<option value="8421">快刻周转</option>
				<option value="8422">记账6+1</option>
                <option value="8423">菠萝游</option>
				<option value="8424">菠萝快达</option>
				<option value="8425">菠萝快跑</option>
				<option value="8426">菠萝周转</option>
				<option value="8427">小花周转</option>
				<option value="8428">小花打卡</option>
				<option value="8429">小花贝贝</option>
				<option value="8020">几多钱Android</option>
				<option value="8021">小时光Android</option>
				<option value="8022">快刻周转Android</option>
				<option value="8430">小花转转</option>
				<option value="8204">香蕉管卡</option>
				<option value="8205">券之家</option>
				<option value="8023">蚂蚁借钱</option>
				<option value="8024">记水账</option>
				<option value="8025">分期周转</option>
				<option value="8026">分期钱包</option>
				<option value="8027">苹果借钱</option>
				<option value="8028">有钱分期</option>
				<option value="8029">有钱周转</option>
				<option value="8030">方便借</option>
				<option value="8031">方便分期</option>
				<option value="16507">借点零花</option>
				<option value="16004">小熊快贷</option>
				<option value="8437">菠萝互帮</option>
				<option value="16007">万能借</option>
				<option value="16513">熊运运</option>
				<option value="8032">橘子借钱</option>
				<option value="16000">熊花花</option>
				<option value="16003">运花花</option>
				<option value="16518">熊熊互助</option>
			</select>
			<div class="item_title">推送类型：</div>
			<select id="message_send_type">
			  <option value="0" selected="selected">单发(指定用户id，推给某一个用户)</option>
			  <option value="1">群发</option>
			</select>
		</div>
		<div>
			<div id="send_special_user_container">
				<div class="item_title">请输入用户id:</div>
				<input  id="userid_input" type="text" />
			</div>
			<div id="send_group_user_container">
				<div class="item_title">选择推送平台:</div>
				<select id="platfrom_type">
				  <option value="-1" selected="selected">所有平台</option>
				  <option value="1">安卓平台</option>
				  <option value="2">IOS平台</option>
				</select>
				<div id="provinces">
					<div class="item_title">省份:</div>
					<select id="province_choose">
						<option value="-1" selected="selected">全部</option>
					</select>
				</div>
				<div class="item_title">用户标签(多个标签使用,分隔):</div>
				<input id="user_tags" type="text" class="text_input"/>
			</div>
			
			<div class="item_title">推送内容:
				<select name="pushcontent" id="pushcontent">
					<option value="CPS">CPS</option>
					<option value="daikuang">贷款</option>
					<option value="chexian">车险</option>
					<option value="jiayouweizhang">加油违章</option>
					<option value="zhidemai">值得买</option>
					<option value="faxianwenzhang">发现文章</option>
					<option value="qita">其他</option>
				</select>
			</div>
			
			<div class="item_title">是否保存到数据库:
				<select name="ismysql" id="ismysql">
					<option value="yes">yes</option>
					<option value="no">no</option>
				</select>
			</div>
			
			<div class="item_title">消息发送时间(不填默认立即发送):</div>
			<input class="form-control date-picker text_input" data-date-format="yyyy-mm-dd hh:ii:ss" type="text" name="send_date" id="send_date" />
			<div class="item_title">消息结束时间:</div>
			<input class="form-control date-picker text_input" data-date-format="yyyy-mm-dd hh:ii:ss" type="text" name="end_date" id="end_date" />
		</div>
		<br/>
		<div id="button_container">
			<button id="send_button" type="button">发送消息</button>
		</div>
		<div id="toast_container">
			<div id="toast_tips"></div>
		</div>
	</body>
</html>

<script type="text/javascript" src="${ctx}/js/call_func.js"></script>
<script type="text/javascript" src="../js/jquery.zclip.min.js"></script> 

<script>
	/**
	 * 获取url请求参数的方法
	 * @param {Object} param_name
	 */
	$.getUrlParam = function(param_name) {
		var reg = new RegExp("(^|&)" + param_name + "=([^&]*)(&|$)");
		var r = window.location.search.substr(1).match(reg);
		if (r != null) return unescape(r[2]);
		return null;
	};
</script>

<script type="text/javascript">
	var debug = true;
	var ALL_USER_ID = -1111;
	//url参数
	var url = "http://" + location.host + "/";
	//要推送的消息体
	var message = decodeURIComponent(decodeURIComponent($.getUrlParam("message")));
	var lockkey = decodeURIComponent(decodeURIComponent($.getUrlParam("lockkey")));
	var record_id = decodeURIComponent(decodeURIComponent($.getUrlParam("record_id")));
	
	var sendproduct = decodeURIComponent(decodeURIComponent($.getUrlParam("sendproduct")));
	var sendtype = decodeURIComponent(decodeURIComponent($.getUrlParam("sendtype")));
	var senduserid = decodeURIComponent(decodeURIComponent($.getUrlParam("senduserid")));
	var push_content = decodeURIComponent(decodeURIComponent($.getUrlParam("push_content")));
	var sendtime = decodeURIComponent(decodeURIComponent($.getUrlParam("sendtime")));
	var endtime = decodeURIComponent(decodeURIComponent($.getUrlParam("endtime")));
	var platform = decodeURIComponent(decodeURIComponent($.getUrlParam("platform")));
	var provinceid = decodeURIComponent(decodeURIComponent($.getUrlParam("provinceid")));
	var tags = decodeURIComponent(decodeURIComponent($.getUrlParam("tags")));
	
	var iosShowNotification = $.getUrlParam("ios_show_notification");
	window.onload = function() {
		//ios通知栏内容 
		var iosLockkeyShow = document.getElementById("ios_lockkey_show");
		iosLockkeyShow.innerHTML = lockkey;
		//要推送的消息体
		var messageShow = document.getElementById("message_show");
		messageShow.innerHTML = message;
		//消息推送类型选择
		var messageSendType = document.getElementById("message_send_type");
		messageSendType.onchange = function() {
			var typeValue = getSelectValueById("message_send_type");
			var sendSpecialUserContainer = document.getElementById("send_special_user_container");
			var sendGroupUserContainer = document.getElementById("send_group_user_container");
			if (typeValue == 0) {
				//单点发
				sendSpecialUserContainer.style.display = "block";
				sendGroupUserContainer.style.display = "none";
			} else if (typeValue == 1) {
				//群发
				sendSpecialUserContainer.style.display = "none";
				sendGroupUserContainer.style.display = "block";
				var provinceChoose = document.getElementById("province_choose");
				if (provinceChoose.childElementCount <= 0) {
					 getProvinces();
				}
			}
		};
		//推送按钮
		var sendButton = document.getElementById("send_button");
		sendButton.onclick = function() {
			var typeValue = getSelectValueById("message_send_type");
			//根据类型进行参数校验
			if (typeValue == 0) {
				//单点发
				var useridInput = document.getElementById("userid_input");
				var userid = useridInput.value;
				if (userid == null || userid.trim() == "") {
					alert("请填写用户id");
					return;
				}
			} else if (typeValue == 1) {
				//群发
			}
			var endDateInput = document.getElementById("end_date");
			var	endDateValue = endDateInput.value;
			if (endDateValue.trim() == "") {
				alert('必须输入结束时间，建议一般3天');
				return;
			} 
			if (window.confirm("确定进行消息推送吗?")) {
				if (typeValue == 0) {
					//单点发
					sendMessage(userid, null, null, null, endDateValue);
				} else if (typeValue == 1) {
					//群发
					var provinceChooseValue = getSelectValueById("province_choose");
					if (provinceChooseValue == -1) {
						//如果是全部省份，就直接推送
						var platfromType = getSelectValueById("platfrom_type");
						sendMessage(ALL_USER_ID, platfromType, null, document.getElementById("user_tags").value, endDateValue);
					} else {
						//如果是单独选择某个省份，就要获取该省份的所有城市，进行标签设置
						getCitysForProvince(provinceChooseValue);
					}
				}
			} else {
			
			}
		};
		//时间控件
		$('.date-picker').datetimepicker({
			autoclose:true,
			startDate: new Date()
		}).next().on(ace.click_event, function(){
			$(this).prev().focus();
		});
		
		
		if (sendproduct!=null && sendproduct!="") {
			$("#message_send_product option[value='"+ sendproduct +"']").attr("selected",true);
		}
		if (sendtype!=null&&sendtype!="") {
			var typeValue = sendtype;
			var sendSpecialUserContainer = document.getElementById("send_special_user_container");
			var sendGroupUserContainer = document.getElementById("send_group_user_container");
			if (typeValue == 0) {
				//单点发
				sendSpecialUserContainer.style.display = "block";
				sendGroupUserContainer.style.display = "none";
			} else if (typeValue == 1) {
				//群发
				sendSpecialUserContainer.style.display = "none";
				sendGroupUserContainer.style.display = "block";
				var provinceChoose = document.getElementById("province_choose");
				if (provinceChoose.childElementCount <= 0) {
					 getProvinces();
				}
			}
			$("#message_send_type option[value='"+sendtype+"']").attr("selected",true);
			if (provinceid!=null&&provinceid!="") {
				$("#province_choose option[value='"+ provinceid +"']").attr("selected",true);
			}
			if (tags!=null&&tags!="") {
				$("#user_tags").val(tags);
			}
		}
		if (senduserid!=null&&senduserid!="") {
			$("#userid_input").val(senduserid);
		}
		if (push_content!=null&&push_content!="") {
			$("#pushcontent option[value='"+ push_content +"']").attr("selected",true);
		}
		if (sendtime!=null&&sendtime!="") {
			$("#send_date").val(sendtime);
		}
		if (endtime!=null&&endtime!="") {
			$("#end_date").val(endtime);
		}
		if (platform!=null&&platform!="") {
			$("#platfrom_type option[value='"+ platform +"']").attr("selected",true);
		}
	};

	/**
	 * 获取下拉列表的值的方法
	 */
	function getSelectValueById(id) {
		var select = document.getElementById(id);
		var selectIndex = select.selectedIndex;
		return select.options[selectIndex].value;
	};
	/**
	 * 获取单选按钮组选择的值的方法
	 */
	function getRadioValueByName(name) {
  		var group = document.getElementsByName(name);
  		if (group != null) {
  			var length = group.length;
	        for(var i = 0; i< length; i++){
	        	   var item = group[i];
	           if(item.checked){
	           	 	return item.value;
	           }
	        }
       	}
        return 0;
	};
	
	/**
	 * 推送的方法
	 */
	function sendMessage(userid, platfrom, provinceids, tags, endTime) {
		var phead = {};
		//请求数据的请求参数
		var send_date = $("#send_date").val();
		var ismysql = $("#ismysql").val();
		var pushcontent = $("#pushcontent").val();
		
		var pushcontent = $("#pushcontent").val();
		var data = '{"lockkey":"' + lockkey + '", "phead":' + JSON.stringify(phead) + ', "msg":' + message;
		if (userid != null) {
			data =  data + ', "userid":"' + userid + '"';
		}
		if (platfrom != null) {
			data =  data + ', "platfrom":"' + platfrom + '"';
		}
		if (provinceids != null) {
			data =  data + ', "provinceids":"' + provinceids + '"';
		}
		if (tags != null) {
			data =  data + ', "tags":"' + tags + '"';
		}
		if (endTime != null) {
			data =  data + ', "expiredate":"' + endTime + '"';
		}
		data =  data + ', "send_date":"' + send_date + '"';
		//产品推送
		var sendProduct = getSelectValueById("message_send_product");
		//-1代表主程序，其它的是各产品对应的productid
		if (sendProduct != -1) {
			//如果不是推给主程序的话，要进行设置，否则就不需要了。默认就是推主程序。
			data =  data + ', "product_id":"' + sendProduct + '"';
		}
		data =  data + '}';
		data = data.replace(new RegExp("&","gm"), "%2526");
		data = data.replace(new RegExp("%","gm"), "%25");
		var requestData = 'request={"data":' + data + '}';
		if (debug) {
			console.log("requestData:" + requestData);
		}
		//请求地址
		var requestUrl = url + "loan_manager/pushmanage/pushmessage.action?record_id=" + record_id + "&ismysql=" + ismysql + "&pushcontent=" + pushcontent + "&send_date=" + send_date;
		$.ajax({
			type: "post",
			timeout: 30000,
			url: requestUrl,
			data: requestData,
			async: true,
			dataType: "json",
			beforeSend: function() {
				showLoading();
			},
			success: function(data) {
				if (debug) {
					console.log("sendMessageToSingle - success");
					console.log("sendMessageToSingle - data:" + JSON.stringify(data));
				}
				if (data != null && data.result != null && data.result.status == 1) {
					alert("消息推送成功");
				} else if (data != null && data.result != null && data.result.status == 2) {
					alert("发送时间必须大于当前时间！");
				} else {
					var message = "消息推送失败";
					if (data != null && data.result != null) {
						message = data.result.msg;
					}
					alert(message);
				}
			},
			error: function(xhr, status, errorThrown) {
				if (debug) {
					console.log("sendMessageToSingle-error");
				}
				alert("网络出错，消息推送失败");
			},
			complete: function(xhr, status) {
				if (debug) {
					console.log("sendMessageToSingle - complete");
				}
				hideLoading();
			}
		});
	};
	
	/**
	 * 获取省份信息的方法
	 
	function getProvinces() {
		//请求地址
		var requestUrl = url + "xmiles-manager/pushmanage/getProvinces.action?rd=" + new Date().getTime();
		$.ajax({
			type: "post",
			timeout: 30000,
			url: requestUrl,
			async: true,
			dataType: "json",
			beforeSend: function() {
				showLoading();
			},
			success: function(data) {
				if (debug) {
					console.log("getProvinces - success");
					console.log("getProvinces - data:" + JSON.stringify(data));
				}
				if (data != null) {
					//获取省份信息成功，初始化群发的省份选择
					initProvincesChoose(data);
				} else {
					toast("获取省份信息失败!");
				}
			},
			error: function(xhr, status, errorThrown) {
				if (debug) {
					console.log("getProvinces-error");
				}
				toast("获取省份信息失败!");
			},
			complete: function(xhr, status) {
				if (debug) {
					console.log("getProvinces-complete");
				}
				hideLoading();
			}
		});
	};
	*/
	/**
	 * 初始化省份选择ui的方法
	 
	function initProvincesChoose(data) {
		var provinces = document.getElementById("provinces");
		var provinceChoose = document.getElementById("province_choose");
		if (data == null) {
			provinces.style.display = "none";
			provinceChoose.innerHTML = "";
		} else {
			provinces.style.display = "block";
			var innerHtml = "";
			innerHtml = innerHtml + "<option value='-1'>全部</option>";
			var length = data.length;
			for (var i = 0; i < length; i++) {
				var item = data[i];
				innerHtml = innerHtml + "<option value='" + item.id + "'>" + item.name + "</option>";
			}
			provinceChoose.innerHTML = innerHtml;
		}
		
	};
	*/
	/**
	 * 获取某个省份的所有城市信息
	 * @param {Object} pid
	 
	function getCitysForProvince(id) {
		//请求地址
		var requestUrl = url + "xmiles-manager/pushmanage/getCityForProvince.action?provinceId=" + id + "&rd=" + new Date().getTime();
		$.ajax({
			type: "post",
			timeout: 30000,
			url: requestUrl,
			async: true,
			dataType: "json",
			beforeSend: function() {
				showLoading();
			},
			success: function(data) {
				if (debug) {
					console.log("getCitysForProvince - success");
					console.log("getCitysForProvince - data:" + JSON.stringify(data));
				}
				if (data != null) {
					//获取城市信息成功，进行推送
					var platfromType = getSelectValueById("platfrom_type");
					//拼接tags
					var userTags = "";
					var userInputTags = document.getElementById("user_tags").value;
					if (userInputTags != null && userInputTags.trim() != "") {
						userTags = userTags + userInputTags;
					}
					var length = data.length;
					for (var i = 0; i < length; i++) {
						var item = data[i];
						if (userTags == "") {
							userTags = userTags + item.id;
						} else {
							userTags = userTags + "," + item.id;
						}
					}
					var endDateInput = document.getElementById("end_date");
					var	endDateValue = endDateInput.value;
					if (endDateValue.trim() == "") {
						endDateValue = null;
					}
					sendMessage(ALL_USER_ID, platfromType, null, userTags, endDateValue);
				} else {
					alert("获取城市信息失败!");
				}
			},
			error: function(xhr, status, errorThrown) {
				if (debug) {
					console.log("getCitysForProvince-error");
				}
				alert("获取城市信息失败!");
			},
			complete: function(xhr, status) {
				if (debug) {
					console.log("getCitysForProvince-complete");
				}
				hideLoading();
			}
		});
	};
	*/
	/**
	 * toast提示
	 * @param {Object} text
	 */
	function toast(text) {
		$('#toast_tips').text(text);
		$('#toast_container').show();
		setTimeout(function() {
			$('#toast_container').hide();
		}, 3000);
	};
	
	/**
	 * 显示loading的方法
	 */
	function showLoading() {
		$(".load_bg").show();
		$(".load_img").show();
	};
	
	/**
	 * 隐藏loading的方法
	 */
	function hideLoading() {
		$(".load_bg").hide();
		$(".load_img").hide();
	};
	
</script>